package exception

import "encoding/json"

func NewApiException(code int, message string) *ApiException {
	return &ApiException{
		Code:    code,
		Message: message,
	}
}

type ApiException struct {
	Code     int    `json:"code"`
	Message  string `json:"message"`
	HttpCode int    `json:"-"`
}

// // The error built-in interface type is the conventional interface for
// // representing an error condition, with the nil value representing no error.
// type error interface {
// 	Error() string
// }

// 实现Error接口，返回ApiException的Message属性
func (e *ApiException) Error() string {
	return e.Message
}

func (e *ApiException) String() string {
	b, _ := json.Marshal(e)
	return string(b)
}

func (e *ApiException) WithHttpCode(code int) *ApiException {
	e.HttpCode = code
	return e
}
